package com.cy.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.entity.User;
import com.cy.entity.UserExample;
import com.cy.mapper.UserMapper;

@Service
public class UserService {

	@Autowired
	private	UserMapper userMapper;
	
	public List<User> findAllUsers(){
		return userMapper.selectByExample(null);
	}
	
	public List<User> findByUserTel(String userTel) {
		UserExample ue = new UserExample();
		ue.createCriteria().andUsertelEqualTo(userTel);
		return userMapper.selectByExample(ue);
	}
	
	public List<User> findUserByAccess(Integer access) {
		UserExample ue = new UserExample();
		ue.createCriteria().andAccessEqualTo(access);
		return userMapper.selectByExample(ue);
	}
	
	public List<User> findByUserTelAndAccess(String tel, Integer access){
		UserExample ue = new UserExample();
		ue.createCriteria().andUsertelLike("%"+tel+"%").andAccessEqualTo(access);
		return userMapper.selectByExample(ue);
	}
	
	public void register(User user) {
		userMapper.insert(user);
	}
	
	public List<User> findUserByUsername(String username) {
		UserExample ue = new UserExample();
		ue.createCriteria().andUsernameEqualTo(username);
		return userMapper.selectByExample(ue);
	}
	
	
	public List<User> findUserByTel(String tel) {
		UserExample ue = new UserExample();
		ue.createCriteria().andUsertelLike("%"+tel+"%");
		return userMapper.selectByExample(ue);
	}
	
	public List<User> login(String tel,String password) {
		UserExample ue = new UserExample();
		ue.createCriteria().andUsertelEqualTo(tel).andPasswordEqualTo(password);
		return userMapper.selectByExample(ue);
	}
	public void updateUserById(User user) {
		userMapper.updateByPrimaryKey(user);
	}
	
	public User findUserById(Integer id) {
		return userMapper.selectByPrimaryKey(id);
	}
	
	public void deleteUser(Integer id) {
		UserExample ue = new UserExample();
		ue.createCriteria().andUseridEqualTo(id);
		userMapper.deleteByExample(ue);
	}
}
